﻿using System;
using System.Data;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using PublicDomain.Data.Csv;

namespace PublicDomain.UnitTest.Data.Csv
{
    [TestClass]
    public class CsvConnectionTest
    {
        public TestContext TestContext { get; set; }

        public void TestInitialize()
        {
        }

        [TestMethod]
        [Description("Tests constructor CsvConnection().")]
        public void CsvConnectionConstructorTest()
        {
            var target = new CsvConnection();

            Assert.IsTrue(String.IsNullOrEmpty(target.ConnectionString));
            Assert.AreEqual(ConnectionState.Closed, target.State);
        }

        [TestMethod]
        [Description("Tests method CsvConnection.Open().")]
        public void OpenTest()
        {
            var target = new CsvConnection();

            target.Open();

            Assert.AreEqual(ConnectionState.Open, target.State);
        }

        [TestMethod]
        [Description("Tests method CsvConnection.Close().")]
        public void CloseTest()
        {
            var target = new CsvConnection();

            target.Close();

            Assert.AreEqual(ConnectionState.Closed, target.State);
        }

        [TestMethod]
        [Description("Tests method CsvConnection.CreateCommand().")]
        public void CreateCommandTest()
        {
            var csvConnection = new CsvConnection();
            var target = csvConnection.CreateCommand();

            Assert.IsInstanceOfType(target, typeof(CsvCommand));
            Assert.AreSame(csvConnection, target.Connection);
        }

        [TestMethod]
        [Description("Tests method CsvConnection.ChangeDatabase(string databaseName).")]
        public void ChangeDatabaseTest()
        {
            var target = new CsvConnection();
            target.ChangeDatabase("C:\\InputData");

            Assert.AreEqual("C:\\InputData", target.Database);
        }

        [TestMethod]
        [Description("Tests method CsvConnection.BeginTransaction(IsolationLevel il).")]
        [ExpectedException(typeof(NotImplementedException))]
        public void BeginTransactionIsolationLevelTest()
        {
            var target = new CsvConnection();
            target.BeginTransaction(IsolationLevel.Unspecified);
        }

        [TestMethod]
        [Description("Tests method CsvConnection.BeginTransaction().")]
        [ExpectedException(typeof(NotImplementedException))]
        public void BeginTransactionTest()
        {
            var target = new CsvConnection();
            target.BeginTransaction();
        }

        [TestMethod]
        [Description("Tests method CsvConnection.Dispose().")]
        public void DisposeTest()
        {
            var target = new CsvConnection();
            target.Dispose();
        }

        [TestMethod]
        [Description("Tests property CsvConnection.ConnectionString.")]
        public void ConnectionStringTest()
        {
            var target = new CsvConnection();

            var expected = "Database=C:\\Temp\\User Abc;Delimiter=,;HeaderRowsCount=1;SchemataFilePath=C:\\Raw Data\\CsvDataFormat.xml;ParsingOptions=EscapedEmptyAsZeroLengthString, AllowLessColumnsInOneRecord";

            target.ConnectionString = expected;

            Assert.AreEqual("database=\"C:\\Temp\\User Abc\";delimiter=,;headerrowscount=1;schematafilepath=\"C:\\Raw Data\\CsvDataFormat.xml\";parsingoptions=\"EscapedEmptyAsZeroLengthString, AllowLessColumnsInOneRecord\"", target.ConnectionString);
        }

        [TestMethod]
        [Description("Tests property CsvConnection.Database.")]
        public void DatabaseTest()
        {
            var target = new CsvConnection();

            Assert.IsNull(target.Database);
        }

        [TestMethod]
        [Description("Tests property CsvConnection.State.")]
        public void StateTest()
        {
            var target = new CsvConnection();

            var state = target.State;

            Assert.AreEqual(ConnectionState.Closed, state);
        }

        [TestMethod]
        [Description("Tests property CsvConnection.ConnectionTimeout.")]
        public void ConnectionTimeoutTest()
        {
            var target = new CsvConnection();
            Assert.AreEqual(-1, target.ConnectionTimeout);
        }
    }
}
